<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use Cookie;
class IndexController extends Controller
{
    //主页面渲染
    public function index(Request $row)
    {
        $k = $row -> key;
        $row = DB::table('house')
            ->join('rent_housetype','house.zh_house_class','=','rent_housetype.rid')
            ->where('zh_name', 'like', '%'.$k.'%')
            ->get();
        return view('index.index', ['row' => $row,'k' => $k]);
    }

    //我的主页渲染
    public function mine()
    {
        $row = DB::table('user') -> where('user_name',Cookie::get('user_name')) -> first();
        if(empty(Cookie::get('user_name')) || $row == null){
           return redirect('/login');
        }
        return view('index.member',['row' => $row]);
    }

    //我关注的房源
    public function focushouse()
    {
        $row = DB::table('user') -> where('user_name',Cookie::get('user_name')) -> get('focushouse_id');
        foreach($row as $rows){$rews = explode(',',$rows -> focushouse_id);}
        for($i=0;$i < count($rews);$i++){$rowsErr[] = DB::table('house') -> where('id',$rews[$i]) -> first();}
        return view('index.focushouse',['rew' => $rowsErr]);

    }

    // 我关注的小区
    public function focuscommunity()
    {
        $row = DB::table('user') -> where('user_name',Cookie::get('user_name')) -> get('focuscommunity_id');
        foreach($row as $rows){$rew = explode(',',$rows -> focuscommunity_id);}
        for($i=0;$i < count($rew);$i++){$rowsErr[] = DB::table('house') -> where('zh_place',$rew[$i]) -> first();}
        return view('index.focuscommunity',['rew' => $rowsErr]);
    }

    //主页推荐房屋页面渲染
    public function housedetail(Request $request)
    {
        $id = $request -> id ;
        $row = DB::table('house')->where('id',$id)->first();
        $aid = $row -> aid ;
        $rows = DB::table('adminuser')->where('id',$aid)->first();
        
        $rrr = DB::table('house')->where('aid',$aid)->limit(3)->get();
     


        return view('index.housedetail',['row' => $row , 'rows' => $rows , 'rrr' => $rrr]);
    }

    //关于我们渲染
    public function contact()
    {
        return view('index.contact');
    }

    //主页登录渲染
    public function login()
    {
        return view('index.login');
    }
    

    //个人信息
    public function self(Request $request)
    {
        $id = $request -> id ;
        $row = DB::table('user')->where('id',$id)->first();
       
        return view('index.self',['row' => $row]);
    }
    //设置
    public function set(Request $request)
    {
        $id = $request -> id ;
        $row = DB::table('user')->where('id',$id)->first();
        return view('index.set',['row' => $row]);
    }
    //设置
    public function set_er(Request $request)
    {
       $input = $request->all();
       $id = $request -> id ;
       DB::table('user') -> where('id',$id) -> update($input);
       return '成功'. @redirect('/mine');
       
    }

    //我的看房记录
    public function checkinglog(Request $request)
    {
       
       return view('index.checkinglog');
    }

    //前台注册页面渲染
    public function register()
    {
        return view('index.register');
    }

    //关于我们渲染
    public function enter()
    {
        return view('index.enter');
    }

    //公司渲染
    public function company()
    {
        return view('index.company');
    }

    //公司文化渲染
    public function cuture()
    {
        return view('index.cuture');
    }

    //加入我们渲染
    public function join()
    {
        return view('index.join');
    }

    //二手房渲染
    public function secondhouse()
    {
        $row = DB::table('house')
        ->join('rent_housetype','house.zh_house_class','=','rent_housetype.rid')
        ->where('zh_tp',1)
        ->where('zh_name', 'like', '%'.Request() -> key.'%')
        ->get();
        return view('index.houselist',['row' => $row]);
    }

    //租房页面渲染
    public function renting(Request $row)
    {
        $row = DB::table('house')
        ->join('rent_housetype','house.zh_house_class','=','rent_housetype.rid')
        ->where('zh_tp',2)
        ->where('zh_name', 'like', '%'.$row -> key.'%')
        ->get();
        return view('index.rentlist',['row' => $row]);
    }

    //经纪人页面渲染
    public function agent()
    {
        $row = DB::table('adminuser')
        ->where('admin_post','房产经纪人')
        ->get();
        return view('index.agent',['row' => $row]);
    }

    //经纪人简介页面渲染
    public function agentdetail()
    {
        $row = DB::table('adminuser')
        ->where('id',Request() -> id)
        ->get();
        $house= DB::table('house')
        ->where('aid',Request() -> id)
        ->get();
        return view('index.agentdetail',['row' => $row,'house' => $house]);
        $row = DB::table('adminuser')
        ->where('admin_post',1)
        ->get();
        return view('index.agentdetail',['row' => $row]);
    }

    //用户注册功能
    public function registerin(Request $row)
    {
        //密码与确认密码是否一致
        if($row -> user_pwd !== $row -> user_repwd)
        {
             return redirect('/register');
        }

        //为空判断
        if(empty($row -> user_name) || empty($row -> user_pwd) || empty($row -> user_repwd) || empty($row -> user_idcard) || empty($row -> user_truename))
        {
            return redirect('/register');
        }

        //身份证是否合法
        if(strlen($row -> user_idcard) < 18)
        {
               return redirect('/register');
        }

        if(substr($row->user_idcard,6,2) != 20 && substr($row->user_idcard,6,2) != 19)
        {
             return redirect('/register');
        }
       //获取现在年份
       $year = date('Y');
       //截取用户输入身份证的出生年份
       $age = substr($row -> user_idcard,6,4);
       //获取用户年龄
       $user_age = $year-$age;
       for($i=0;$i<10;$i++)
       {
            static $user_account = [];
            $user_account[] = rand()%10;
       }
       $u = implode($user_account);
       $user = array(
            'user_name' => $row -> user_name,
            'user_pwd'  => md5($row -> user_pwd),
            'user_repwd'  => md5($row -> user_repwd),
            'create_time' => date('Y-m-d h:m:s'),
            'user_truename' => $row -> user_truename,
            'user_account' => $u,
            'user_idcard' => $row -> user_idcard,
            'user_age' => $user_age,
       );
       //加入数据库
       DB::table('user') -> insert($user);
       return redirect('/login');
    }

    public function loginin(Request $row)
    {
        //为空判断
        if(empty($row -> user_account) || empty($row -> user_pwd))
        {
            return redirect('/login');
        }

        $user = DB::table('user') -> where('user_account',$row -> user_account) -> where('user_pwd',md5($row -> user_pwd)) -> first();
        if($user)
        {
            return redirect('/mine') -> cookie('user_name',$user -> user_name);
        }
            return redirect('/login');
    }

    //用户退出功能
     public function login_out(){
        $cookie = Cookie::forget('user_name');
        return redirect('/mine')->withCookie($cookie);
     }



}
